<template>
  <el-form ref="roleFormRef" class="demo-ruleForm" status-icon :model="addForm"
           :rules="loginRules">
    <el-form-item label="姓名" prop="name" label-width="100px">
      <el-input v-model="addForm.name" placeholder="请输入姓名"/>
    </el-form-item>
    <el-form-item label="邮箱" prop="mail" label-width="100px">
      <el-input v-model="addForm.mail" placeholder="请输入邮箱地址"/>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</template>

<script lang="ts" setup>
  import {defineProps, ref} from "vue";

  import {addStation,updateStation} from "@/api/site";
  import {ElMessage} from "element-plus";
  import {addMail, updateMail} from "@/api/message";

  const props = defineProps({data: {}})

  const addForm = ref(Object.assign({},props.data))
  //@ts-ignore
  let isUpdate=!!addForm.value.id
  const loginRules = {
    name: [{required: true, trigger: 'blur', message: '请输入姓名'}],
    mail: [{required: true, trigger: 'blur', message: '请输入邮箱'}],
  }

  const submit=()=>{
    if (isUpdate){
      updateMail(addForm.value)
        .then(resp => {
          if (resp.code == 200) {
            ElMessage.success('更新成功')
          } else {
            ElMessage.error('更新失败')
          }
        })
    }else {
      addMail(addForm.value)
        .then(resp => {
          if (resp.code == 200) {
            ElMessage.success('添加成功')
          } else {
            ElMessage.error('添加失败')
          }
        })
    }
  }
</script>

<style scoped lang="scss">
.title {
  font-size: 15px;
  margin: 10px 0;
  padding-left: 5px;
  color: #1A88F1;
  border-left: 3px solid #1A88F1;
}
</style>
